/**
 * @Copyright 2008 版权归陈仁飞，不要肆意侵权抄袭，如引用请注明出处保留作者信息。
 */
package hotel.base.oversea.utils;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 *
 * @description 日期处理支持类，提供日常工作中的所有日期的操作处理
 */
@SuppressWarnings({ "unused" })
public class DateUtil {
	/**
	 * 定义日志
	 */
	private final static Logger logger = LogManager.getLogger(DateUtil.class);

	private static final String[] CHINA_DATE_KEYS = { "○", "О", "0", "Ο", "O", "零", "一", "二", "三", "四", "五", "六", "七",
			"八", "九", "十", "年", "月", "日", "时", "分", "秒" };
	private static final String[] CHINA_DATE_KEY_MAP = { "0", "0", "0", "0", "0", "0", "1", "2", "3", "4", "5", "6",
			"7", "8", "9", "10", "-", "-", " ", ":", ":", " " };

	/**
	 * 英文月份名称
	 */
	private static final String[] MONTH_ENGLISH_NAME = { "January", "February", "March", "April", "May", "June", "July",
			"August", "September", "October", "November", "December" };

	/**
	 * 中文星期的名称
	 */
	private static final String[] WEEK_CHINA_NAME = { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" };

	/**
	 * 英文日期的几种格式
	 */
	private static final Collection<String> DEFAULT_PATTERNS = Arrays
			.asList(new String[] { "EEE MMM d HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss Z yyyy", "EEE MMM dd Z yyyy",
					"EEE MMM dd yyyy", "EEEE, dd-MMM-yy HH:mm:ss zzz", "EEE, dd MMM yyyy HH:mm:ss zzz" });

	/**
	 * 获取day天之后的日期
	 * @param day 天数
	 * @return
	 */
	public static String getDate(int day){
		Calendar calendar1 = Calendar.getInstance();
		calendar1.add(Calendar.DATE, day);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		return sdf.format(calendar1.getTime());
	}
    /**
	 * 定义日期的格式
	 */
	public final static class FORMAT {
		public final static String YEAR_MONTH = "yyyy-MM";

		/**
		 * 6位日期格式
		 */
		public final static String DATE_6CHAR = "yyMMdd";

		/**
		 * 8位日期格式
		 */
		public final static String DATE_8CHAR = "yyyyMMdd";

		/**
		 * 点号日期格式
		 */
		public final static String DATE_DOT = "yyyy.MM.dd";

		/**
		 * 反斜杠日期格式
		 */
		public final static String DATE_SLASH = "yyyy/MM/dd";

		/**
		 * 横杠日期格式
		 */
		public final static String DATE_HORIZONTAL = "yyyy-MM-dd";

		/**
		 * 日期时间(日期点号分割)
		 */
		public final static String DATATIME_DOT = "yyyy.MM.dd HH:mm:ss";

		/**
		 * 日期时间(日期反斜杠)
		 */
		public final static String DATETIME_SLASH = "yyyy/MM/dd HH:mm:ss";

		/**
		 * 日期时间(日期横杠)
		 */
		public final static String DATETIME_HORIZONTAL = "yyyy-MM-dd HH:mm:ss";
	}

	/**
	 * @todo 将日期字符串或时间转换成时间类型 日期字符串中的日期分隔符可是:"/",".","-"， 返回时间具体到秒 只提供常用的日期格式处理
	 * @param data
	 * @return Date
	 */
	public static Date parse(Object data, String format) {
		return parse(data, format, null);
	}

	public static Date parse(Object data, String format, String local) {
		if (null == data) {
			logger.error("The date string is null!");
			return null;
		}
		if (data instanceof String) {
			String dateString = data.toString().trim();
			if ("".equals(dateString) || "null".equals(dateString.toLowerCase())) {
				logger.error("The date string is null!");
				return null;
			}
			return parseString(dateString, format, local);
		} else {
			String result = formatDate(data, format, local);
			return parseString(result, format, local);
		}
	}

	public static Date parseString(String dateStr) {
		return parseString(dateStr, null, null);
	}

	/**
	 * @todo 将日期字符串或时间转换成时间类型 日期字符串中的日期分隔符可是:"/",".","-"， 返回时间具体到秒 只提供常用的日期格式处理
	 * @param dateStr
	 * @return
	 */
	public static Date parseString(String dateStr, String dateFormat, String locale) {
		if (dateStr == null)
			return null;
		dateStr = dateStr.trim();
		if ("".equals(dateStr))
			return null;
		String realDF = null;
		if (StringUtil.isNotBlank(dateFormat)) {
			realDF = dateFormat;
		} // 英文日期格式
		else if (StringUtil.matches(dateStr, "[a-zA-Z]")) {
			SimpleDateFormat dateParser = null;
			Iterator<String> formatIter = DEFAULT_PATTERNS.iterator();
			Date result = null;
			while (formatIter.hasNext()) {
				String format = (String) formatIter.next();
				if (dateParser == null) {
					dateParser = new SimpleDateFormat(format, Locale.US);
				} else {
					dateParser.applyPattern(format);
				}
				try {
					result = dateParser.parse(dateStr);
					if (result != null)
						break;
				} catch (ParseException pe) {
				}
			}
			return result;
		} else {
			// 中文日期格式
			if (StringUtil.matches(dateStr, "[年月日时分秒]"))
				dateStr = parseChinaDate(dateStr);
			// 含中文，但非标准的时间性中文
			else if (StringUtil.hasChinese(dateStr)) {
				return null;
			}
			int size;
			boolean hasBlank = (dateStr.indexOf(" ") != -1);
			int splitCount;
			int startIndex;
			// 日期和时间的组合
			if (hasBlank) {
				// 统一格式(去除掉日期中的符号变成全数字)
				dateStr = dateStr.replaceFirst("\\s+", " ").replace("-", "").replace(".", "").replace(":", "")
						.replace("/", "");
				int preSize = dateStr.indexOf(" ");
				size = dateStr.length();
				if (size > 16) {
					realDF = "yyyyMMdd HHmmssSSS";
				} else if (size == 16) {
					if (preSize == 8)
						realDF = "yyyyMMdd HHmmssS";
					else
						realDF = "yyMMdd HHmmssSSS";
				} else if (size == 13) {
					if (preSize == 8)
						realDF = "yyyyMMdd HHmm";
					else
						realDF = "yyMMdd HHmmss";
				} else if (size == 11) {
					if (preSize == 8)
						realDF = "yyyyMMdd HH";
					else
						realDF = "yyMMdd HHmm";
				} else if (size == 9)
					realDF = "yyMMdd HH";
				else
					realDF = "yyyyMMdd HHmmss";
			} else {
				// 去除数字中带的,例如:201,512
				dateStr = dateStr.replace(",", "");
				size = dateStr.length();
				if (dateStr.indexOf(":") != -1) {
					if (dateStr.indexOf(".") != -1) {
						realDF = "HH:mm:ss.SSS";
					} else {
						if (size == 5)
							realDF = "HH:mm";
						else
							realDF = "HH:mm:ss";
					}
				} else {
					dateStr = dateStr.replace("-", "/").replace(".", "/");
					splitCount = StringUtil.matchCnt(dateStr, "\\/");
					if (splitCount == 2) {
						startIndex = dateStr.indexOf("/");
						if (startIndex == 2)
							realDF = "yy/MM/dd";
						else
							realDF = "yyyy/MM/dd";
					} else if (splitCount == 1) {
						if (size > 5)
							realDF = "yyyy/MM";
						else
							realDF = "yy/MM";
					} else {
						if (size >= 15)
							realDF = "yyyyMMddHHmmssSSS";
						else if (size == 14)
							realDF = "yyyyMMddHHmmss";
						else if (size == 12)
							realDF = "yyMMddHHmmss";
						else if (size == 10)
							realDF = "yyyyMMddHH";
						else if (size == 6)
							realDF = "yyyyMM";
						else if (size == 4)
							realDF = "yyyy";
						else
							realDF = "yyyyMMdd";
					}
				}
			}
			if (realDF == null) {
				realDF = hasBlank ? "yyyy-MM-dd HH:mm:ss" : "yyyy-MM-dd";
			}
		}
		DateFormat df = (StringUtil.isBlank(locale)) ? new SimpleDateFormat(realDF)
				: new SimpleDateFormat(realDF, new Locale(locale));
		try {
			return df.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date convertDateObject(Object dt) {
		return convertDateObject(dt, null, null);
	}
	/**
	 * 获取当前时间的下N天
	 * @param days
	 */
	public static String getNextDays(Integer days){
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date temp = new Date();
		Calendar cld = Calendar.getInstance();
		cld.setTime(temp);
		cld.add(Calendar.DATE, days);
		temp = cld.getTime();
		//获得下一天日期字符串
		return dft.format(temp);
	}

	/**
	 * @todo 日期对象类型转换
	 * @param dt
	 * @param format
	 * @param local
	 * @return
	 */
	public static Date convertDateObject(Object dt, String format, String local) {
		if (dt == null) {
			System.err.println("日期不能为空,请正确输入!");
			return null;
		}
		if (!(dt instanceof String) && !(dt instanceof java.sql.Date) && !(dt instanceof Date)
				&& !(dt instanceof Number))
			throw new IllegalArgumentException(dt + "日期数据必须是String、Date、Long、Integer类型,请正确输入!");
		Date result = null;
		String dtStr = dt.toString();
		if (dt instanceof String) {
			result = parseString(dtStr, format, local);
		} else if (dt instanceof Date)
			result = new Date(((Date) dt).getTime());
		else if (dt instanceof java.sql.Date)
			result = new Date(((java.sql.Date) dt).getTime());
		else if (dt instanceof Number) {
			// 13位表示毫秒数
			if (dtStr.length() != 13)
				result = parseString(dtStr, format, local);
			else
				result = new Date(((Number) dt).longValue());
		} else
			result = parseString(dtStr, format, local);
		return result;
	}

	/**
	 * @todo 格式化日期
	 * @param dt
	 * @param fmt
	 * @return
	 */
	public static String formatDate(Object dt, String fmt) {
		return formatDate(dt, fmt, null);
	}

	public static String formatDate(Object dt, String fmt, String locale) {
		if (dt == null)
			return null;
		if (fmt.equalsIgnoreCase("MM"))
			return Integer.toString(getMonth(dt));
		else if (fmt.equalsIgnoreCase("dd"))
			return Integer.toString(getDay(dt));
		DateFormat df = StringUtil.isBlank(locale) ? new SimpleDateFormat(fmt)
				: new SimpleDateFormat(fmt, new Locale(locale));
		Date tmp = convertDateObject(dt, null, locale);
		return (null == tmp) ? null : df.format(tmp);
	}

	public static String formatDate(Object dt, String fmt, String targetFmt, String locale) {
		Date result = parse(dt, fmt, locale);
		return formatDate(result, targetFmt);
	}

	/**
	 * @todo 获取当前以sql.date的日期
	 * @param date
	 * @return
	 */
	public static java.sql.Date getSqlDate(Object date) {
		return new java.sql.Date(convertDateObject(date == null ? new Date() : date).getTime());
	}

	public static Timestamp getTimestamp(Object date) {
		return new Timestamp(date == null ? System.currentTimeMillis() : convertDateObject(date).getTime());
	}

	/**
	 * @todo 获取当前操作系统的时间
	 * @return 当前操作系统的时间
	 */
	public static Date getNowTime() {
		return Calendar.getInstance().getTime();
	}

	/**
	 * @todo 获取当前的时间 返回 YYYY-mm-dd HH:mm:ss 格式字符串
	 * @return 当前操作系统的时间
	 */
	public static String getCurrTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss", null);
	}

	/**
	 * @todo 获取当前的时间 返回指定格式
	 * @return 当前操作系统的时间
	 */
	public static String getCurrTimeYmd(String ymd) {
		return formatDate(new Date(), ymd, null);
	}

	/**
	 * @todo 获取当前的时间 返回指定格式
	 * @return 当前操作系统的时间
	 */
	public static String getCurrStrAdd(Integer d,String ymd) {
		return DateUtil.formatDate(DateUtil.addDay(new Date(),d),ymd);
	}

	/**
	 * @todo 获取当前的时间 返回指定格式
	 * @return 当前操作系统的时间
	 */
	public static String getDateStrAdd(Object objectDate,Integer d,String ymd) {
		return DateUtil.formatDate(DateUtil.addDay(objectDate,d),ymd);
	}

	/*
	 * 将时间转换为时间戳
	 */
	public static String dateToStamp(String s){
        String res = null;
	    try {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = simpleDateFormat.parse(s);
            long ts = date.getTime();
            res = String.valueOf(ts);
        }catch (Exception e){
	        e.printStackTrace();
        }
		return res;
	}

	// Add millisecond
	public static Date addMilliSecond(Object dt, long millisecond) {
		Date result = convertDateObject(dt);
		if (millisecond != 0)
			result.setTime(result.getTime() + millisecond);
		return result;
	}

	public static Date addSecond(Object dt, double second) {
		Double millisecond = new Double(1000.0 * second);
		return addMilliSecond(dt, millisecond.longValue());
	}

	public static Date addDay(Object dt, long day) {
		return addMilliSecond(dt, 1000L * 60L * 60L * 24L * day);
	}

	public static Date addDay(Object dt, double day) {
		Double millisecond = new Double(1000.0 * 60.0 * 60.0 * 24.0 * day);
		return addMilliSecond(dt, millisecond.longValue());
	}

	// add month
	public static Date addMonth(Object dt, int month) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(convertDateObject(dt));
		gc.add(2, month);
		return gc.getTime();
	}

	public static int getYear(Object dateValue) {
		GregorianCalendar currentDate = new GregorianCalendar();
		if (dateValue != null)
			currentDate.setTime(convertDateObject(dateValue));
		return currentDate.get(Calendar.YEAR);
	}

	public static int getMonth(Object dateValue) {
		GregorianCalendar currentDate = new GregorianCalendar();
		if (dateValue != null)
			currentDate.setTime(convertDateObject(dateValue));
		return currentDate.get(Calendar.MONTH) + 1;
	}

	public static int getDay(Object dateValue) {
		GregorianCalendar currentDate = new GregorianCalendar();
		if (null != dateValue)
			currentDate.setTime(convertDateObject(dateValue));
		return currentDate.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * @todo 获取两个时间间隔的天数
	 * @param floorDate
	 * @param goalDate
	 * @return
	 */
	public static int getIntervalDays(Object floorDate, Object goalDate) {
		BigDecimal result = new BigDecimal(
				new Double(getIntervalMillSeconds(DateUtil.formatDate(floorDate, FORMAT.DATE_HORIZONTAL),
						DateUtil.formatDate(goalDate, FORMAT.DATE_HORIZONTAL))) / (3600 * 1000 * 24));
		return result.setScale(1, BigDecimal.ROUND_HALF_UP).intValue();
	}

	/**
	 * @todo 获取两时间间隔的毫秒数
	 * @param floorDate
	 * @param goalDate
	 * @return
	 */
	public static long getIntervalMillSeconds(Object floorDate, Object goalDate) {
		return convertDateObject(goalDate).getTime() - convertDateObject(floorDate).getTime();
	}

	/**
	 * @todo 将日期转化为中文格式
	 * @param dateValue
	 * @return
	 */
	public static String format2China(Object dateValue) {
		Date date = convertDateObject(dateValue);
		if (null == date)
			return null;
		GregorianCalendar pointDate = new GregorianCalendar();
		pointDate.setTime(convertDateObject(dateValue));
		String tmpDate;
		StringBuilder result = new StringBuilder();
		if (dateValue instanceof String) {
			tmpDate = (String) dateValue;
			if (tmpDate.length() >= 4)
				result.append(pointDate.get(Calendar.YEAR) + "年");
			if (tmpDate.length() >= 6)
				result.append((pointDate.get(Calendar.MONTH) + 1) + "月");
			if (tmpDate.length() >= 8)
				result.append(pointDate.get(Calendar.DAY_OF_MONTH) + "日");
			if (tmpDate.length() > 10) {
				result.append(pointDate.get(Calendar.HOUR_OF_DAY) + "时");
				result.append(pointDate.get(Calendar.MINUTE) + "分");
				result.append(pointDate.get(Calendar.SECOND) + "秒");
			}
		} else {
			result.append(pointDate.get(Calendar.YEAR) + "年");
			result.append((pointDate.get(Calendar.MONTH) + 1) + "月");
			result.append(pointDate.get(Calendar.DAY_OF_MONTH) + "日");
			result.append(pointDate.get(Calendar.HOUR_OF_DAY) + "时");
			result.append(pointDate.get(Calendar.MINUTE) + "分");
			result.append(pointDate.get(Calendar.SECOND) + "秒");
		}
		return result.toString();
	}

	/**
	 * @todo 转换中文日期为指定格式的英文日期形式
	 * @param chinaDate
	 * @param fmt
	 * @return
	 */
	public static String parseChinaDate(String chinaDate, String fmt) {
		if (StringUtil.isBlank(chinaDate))
			return null;
		// 去除中文日期文字之间的空格
		String tmp = chinaDate.replaceAll("\\s+", "");
		for (int i = 0; i < CHINA_DATE_KEYS.length; i++) {
			tmp = tmp.replaceAll(CHINA_DATE_KEYS[i], CHINA_DATE_KEY_MAP[i]);
		}
		tmp = tmp.replace("整", "").trim();
		if (tmp.endsWith("-") || tmp.endsWith(":"))
			tmp = tmp.substring(0, tmp.length() - 1);
		if (StringUtil.isBlank(fmt))
			return tmp.toString();
		else
			return formatDate(tmp, fmt);
	}

	/**
	 * @todo 转换中文日期为英文格式
	 * @param chinaDate
	 * @return
	 */
	public static String parseChinaDate(String chinaDate) {
		return parseChinaDate(chinaDate, null);
	}

	/**
	 * @todo 获取月份的第一天
	 * @param objectDate
	 * @return
	 */
	public static Date firstDayOfMonth(Object objectDate) {
		Date date = convertDateObject(objectDate);
		if (null == date)
			return null;
		else {
			String tmp = formatDate(date, FORMAT.DATE_HORIZONTAL);
			return parse(tmp, FORMAT.YEAR_MONTH);
		}
	}

	/**
	 * @todo 获取月份的最后一天
	 * @param objectDate
	 * @return
	 */
	public static Date lastDayOfMonth(Object objectDate) {
		Date date = convertDateObject(objectDate);
		if (null == date)
			return null;
		else {
			String tmp = formatDate(date, FORMAT.DATE_HORIZONTAL);
			Date result = parse(tmp, FORMAT.YEAR_MONTH);
			result = addMonth(result, 1);
			result = addDay(result, -1);
			return result;
		}
	}

	/**
	 * 获取间隔的小时数 分数
	 * @param startTime
	 * @param endTime
	 * @param str
	 * @param format
	 * @return
	 */
	public static Long dateDiff(String startTime, String str,String endTime, String format) {
		// 按照传入的格式生成一个simpledateformate对象
		if(StringUtils.isEmpty(format)){
			format="HH:mm:ss";
		}
		if(StringUtils.isEmpty(endTime)){
			endTime="24:00:00";
		}
		SimpleDateFormat sd = new SimpleDateFormat(format);
		long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
		long nh = 1000 * 60 * 60;// 一小时的毫秒数
		long nm = 1000 * 60;// 一分钟的毫秒数
		long diff;
		long day = 0;
		long hour = 0;
		long min = 0;
		// 获得两个时间的毫秒时间差异
		try {
			diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
			day = diff / nd;// 计算差多少天
			hour = diff % nd / nh + day * 24;// 计算差多少小时
			min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (str.equalsIgnoreCase("h")) {
			return hour;
		} else if (str.equalsIgnoreCase("day")) {
			return day;
		}else{
			return min;
		}
	}

	public static int compareDate(String DATE1, String DATE2,String format) {
		DateFormat df = new SimpleDateFormat(format);
		try {
			Date dt1 = df.parse(DATE1);
			Date dt2 = df.parse(DATE2);
			if (dt1.getTime() > dt2.getTime()) {
				//System.out.println("dt1 在dt2前");
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				//System.out.println("dt1在dt2后");
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取今天开始时间
	 */
	public Date getStartTime(){
		Calendar today=Calendar.getInstance();
		today.set(Calendar.HOUR, 0);
		today.set(Calendar.MINUTE, 0);
		today.set(Calendar.SECOND, 0);
		today.set(Calendar.MILLISECOND, 0);
		return today.getTime();
	}

	/**
	 * 获取今日结束时间
	 */
	public Date getEndTime(){
		Calendar today=Calendar.getInstance();
		today.set(Calendar.HOUR, 23);
		today.set(Calendar.MINUTE, 59);
		today.set(Calendar.SECOND, 59);
		today.set(Calendar.MILLISECOND, 999);
		return today.getTime();
	}

	 /**
	   * 返回两个时间间隔内的所有日期 (包含开始时间和结束时间)
	   *
	   * @param dateStart eg：2017-1-1
	   * @param dateEnd   eg:2019-9-25
	   * @return java.util.List<java.lang.String>
	   * @Title: getTwoDaysDay
	   */
	 public static Map<String,String> getTwoDaysDay(String dateStart, String dateEnd) {
	 	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	 	Map<String,String> dateList = new HashMap<>();
	 	try {
			 Date dateOne = sdf.parse(dateStart);
			 Date dateTwo = sdf.parse(dateEnd);

			 Calendar calendar = Calendar.getInstance();
			 calendar.setTime(dateOne);

			 dateList.put(sdf.format(dateOne),sdf.format(dateOne));
			 while (calendar.getTime().before(dateTwo)) {
					 calendar.add(Calendar.DAY_OF_MONTH, +1);
					 String calendarGetTime = sdf.format(calendar.getTime());
					 dateList.put(calendarGetTime,calendarGetTime);
				 }
		 } catch (Exception e) {
			 e.printStackTrace();
		 }
	 	 return dateList;
	}

	public static Map<String,String> getTwoDaysDayIndex(String dateStart, String dateEnd) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Map<String,String> dateList = new HashMap<>();
		try {
			Date dateOne = sdf.parse(dateStart);
			Date dateTwo = sdf.parse(dateEnd);

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(dateOne);

			dateList.put(sdf.format(dateOne),sdf.format(dateOne));
			int i = 1;
			while (calendar.getTime().before(dateTwo)) {
				calendar.add(Calendar.DAY_OF_MONTH, +1);
				String calendarGetTime = sdf.format(calendar.getTime());
				dateList.put(calendarGetTime,String.valueOf(i));
				i+=1;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateList;
	}

	public static String parseStrYYYYMMDD(String dateStr) throws ParseException {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		formatter.setLenient(false);
		Date newDate= formatter.parse(dateStr);
		formatter = new SimpleDateFormat("yyyy-MM-dd");
		String time = formatter.format(newDate);
		return time;
	}


	/**
	 * 对接收的携程日期进行格式化
	 * /Date(1384171247000+0800)/ 转换为yyyy-mm-dd
	 * @param str
	 */
	public static String  switchDateParse(String str){
		String timeStr = "";
		if(StringUtil.isNotBlank(str)) {
			if(str.indexOf("/Date") != -1){
				str=str.replace("/Date(","").replace(")/","");
				String time=str.substring(0,str.length()-5);
				Date date=new Date(Long.parseLong(time));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				timeStr = format.format(date);
			}else{
				timeStr = str;
			}
		}
		return timeStr;
	}


	/**
	 * 对接收的携程日期进行格式化
	 * /Date(1384171247000+0800)/ 转换为yyyy-MM-dd HH:mm:ss
	 * @param str
	 */
	public static String  switchTimeParse(String str){
		String timeStr = "";
		if(StringUtil.isNotBlank(str)) {
			if(str.indexOf("/Date") != -1){

				str=str.replace("/Date(","").replace(")/","");
				String time=str.substring(0,str.length()-5);
				Date date=new Date(Long.parseLong(time));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				timeStr = format.format(date);
			}else{
				timeStr = str;
			}
		}
		return timeStr;
	}


	/**
	 * 对接收的携程日期进行格式化 并做两个时间差值
	 * /Date(1384171247000+0800)/ 转换为yyyy-MM-dd HH:mm:ss
	 * @param mTime
	 * @param sTime
	 * @return 秒
	 */
	public static double switchTimeSubtractParse(String mTime,String sTime){
		mTime=mTime.replace("/Date(","").replace(")/","");
		String minuend=mTime.substring(0,mTime.length()-5);
		sTime=sTime.replace("/Date(","").replace(")/","");
		String sub=sTime.substring(0,sTime.length()-5);
		long time = Long.parseLong(minuend)-Long.parseLong(sub);
		DecimalFormat df = new DecimalFormat("######0.00");
		BigDecimal bd = new BigDecimal("0");
		System.out.println("time:"+time);
		System.out.println("Float:"+Float.valueOf(time/1000));
		return Double.parseDouble(df.format(Double.valueOf(time/1000)));
	}

	/**
	 * 获取当前时间的下一天
	 * @param day
	 */
	public static String getNextDay(String day){
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		String nextDay="";
		try {
			Date temp = dft.parse(day);
			Calendar cld = Calendar.getInstance();
			cld.setTime(temp);
			cld.add(Calendar.DATE, 1);
			temp = cld.getTime();
			//获得下一天日期字符串
			nextDay = dft.format(temp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nextDay;
	}

	/**
	 * 获取下一月的今天
	 */
	public static String getNextMothesDay(String day){
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		String nextDay="";
		try {
			Date temp = dft.parse(day);
			Calendar cld = Calendar.getInstance();
			cld.setTime(temp);
			cld.add(Calendar.MONTH, 1);
			temp = cld.getTime();
			//获得下一天日期字符串
			nextDay = dft.format(temp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nextDay;
	}
	/**
	 * 获取15天后的日期
	 */
	public static String getNextFifteenDay(String day){
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		String nextDay="";
		try {
			Date temp = dft.parse(day);
			Calendar cld = Calendar.getInstance();
			cld.setTime(temp);
			cld.add(Calendar.DATE, 15);
			temp = cld.getTime();
			//获得下一天日期字符串
			nextDay = dft.format(temp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nextDay;
	}

	/**
	 * 获取当前时间的前俩天
	 * @param day
	 */
	public static String getFrontDay(String day){
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		String nextDay="";
		try {
			Date temp = dft.parse(day);
			Calendar cld = Calendar.getInstance();
			cld.setTime(temp);
			cld.add(Calendar.DATE, -2);
			temp = cld.getTime();
			//获得下一天日期字符串
			nextDay = dft.format(temp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nextDay;
	}

	/**
	 * 获取当前时间的前俩天
	 * @param day
	 */
	public static String getFontsDay(String day){
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		String nextDay="";
		try {
			Date temp = dft.parse(day);
			Calendar cld = Calendar.getInstance();
			cld.setTime(temp);
			cld.add(Calendar.DATE, -1);
			temp = cld.getTime();
			//获得下一天日期字符串
			nextDay = dft.format(temp);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return nextDay;
	}

    /**
     * 获取指定时间的前几天
     * index  为负数
     * @param day
     */
    public static String getFrontDay(String day,Integer index){
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        String nextDay="";
        try {
            Date temp = dft.parse(day);
            Calendar cld = Calendar.getInstance();
            cld.setTime(temp);
            cld.add(Calendar.DATE, index);
            temp = cld.getTime();
            //获得下一天日期字符串
            nextDay = dft.format(temp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return nextDay;
    }

	/**
	 * 获取今天
	 * @return String
	 * */
	public static String getToday() {
		return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
	}

	public static String getToday2() {
		return new SimpleDateFormat("yyyyMMdd").format(new Date());
	}
	/**
	 * 获取指定时间的上一天
	 */
	public static String getUpDate(String time,int n){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance();
		Date date=null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		calendar.setTime(date);
		int day=calendar.get(Calendar.DATE);
		//                      此处修改为+1则是获取后一天
		calendar.set(Calendar.DATE,day-n);

		String lastDay = sdf.format(calendar.getTime());
		return lastDay;
	}

	/**
	 * 比较时间大小
	 * @param beginDay
	 * @param endTime
	 * @return
	 */
	public static boolean commpareDay(String beginDay,String endTime) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date et= null;
		try {
			et = sdf.parse(endTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date bt= null;
		try {
			bt = sdf.parse(beginDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(bt.before(et)||bt.equals(et)){
			return true;
		}else{
			return false;
		}
	}
	public static boolean commpareDay2(String beginDay,String endTime) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
		Date et= null;
		try {
			et = sdf.parse(endTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		Date bt= null;
		try {
			bt = sdf.parse(beginDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(bt.before(et)){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * @author: lmf
	 * @date: 2020/8/4 14:08
	 * 获取俩个日期间隔天数
	 */
	public static int getDay(String startTime,String endTime) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate= null;
		Date endDate=null;
		try {
			startDate = format.parse(startTime);
			endDate=format.parse(endTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return (int)((endDate.getTime()-startDate.getTime())/(1000*3600*24));
	}

	/**
	 * @author: lmf
	 * @date: 2020/8/18 15:51
	 * 根据当前日期判断星期几
	 */
	public static String dayForWeek(String time){
		String[] weeks={"周一","周二","周三","周四","周五","周六","周日"};
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Calendar c=Calendar.getInstance();
		int day=0;
		try {
			c.setTime(format.parse(time));
			if(c.get(Calendar.DAY_OF_WEEK)==1){
				day=7;
			}else{
				day=c.get(Calendar.DAY_OF_WEEK)-1;
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return weeks[day-1];
	}
	/**
	 * 获得指定日期的后几分钟
	 * @author: lmf
	 * @date: 2020/9/7 15:47
	 * @param specifiedDay
	 * @return
	 * @throws Exception
	 */
	public static String getSpecifiedSecondBefore(String specifiedDay) {
		Calendar c = Calendar.getInstance();
		Date date = null;
		try {
			date = new SimpleDateFormat("HH:mm:ss").parse(specifiedDay);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		c.setTime(date);
		int day = c.get(Calendar.MINUTE);
		c.set(Calendar.MINUTE, day + 60);

		String dayBefore = new SimpleDateFormat("HH:mm:ss").format(c
				.getTime());
		return dayBefore;
	}

	/***
	* @Author: lmf
	* @Date: 2020/12/21
	*/
	/**
	 * 得到几天后的时间
	 * @param d
	 * @param day
	 * @return
	 */
	public static String getDateAfter(Date d,int day) {
		Calendar now = Calendar.getInstance();
		now.setTime(d);
		now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.format(now.getTime());
	}
	/***
	* @Author: lmf
	* @Date: 2020/12/21
	 *@time: 2020/10/15 10:18
	 *比如给14:00 + 120分钟 返回 16：00
	*/
	public static String getMinuteByPointTime(String day,int time){
		SimpleDateFormat format=new SimpleDateFormat("HH:mm");
		Date date=null;
		try {
			date=format.parse(day);
		}catch (Exception e){
			e.printStackTrace();
		}
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE,time);
		date=calendar.getTime();
		return format.format(date);
	}

	/***
	* @Author: lmf
	* @Date: 2021/1/11
	 * 获取几天的前的日期
	*/
	public static String getAgoDay(int day) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Calendar c=Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE,-day);
		Date d=c.getTime();
		return  format.format(d);
	}

	/***
	 * @Author: lmf
	 * @Date: 2021/1/11
	 * 获取几天的前的日期
	 */
	public static String getAgoDays(int day) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c=Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE,-day);
		Date d=c.getTime();
		String time=format.format(d)+" 00:00:00";
		return  time;
	}



	public static void main(String[] args) {
		String time=DateUtil.getAgoDays(1);
		System.out.println(time);
		//startTime>endTime false startTime<=endTime true
		String startTime="2020-12-27";
		String endTime="2020-12-26";
		if(DateUtil.commpareDay(startTime,endTime)){
			System.out.print(startTime+"小于等于");
		}else{
			System.out.print("15天之内");
		}
		String data=DateUtil.getUpDate("2020-11-02",1);
	/*	String time="2020-08-16";
		String startDate = DateUtil.formatDate(new Date(),"yyyy-MM-dd");
		String day=getFontsDay(startDate);
		System.out.println(day);*/
/*	    String date="2019-10-18";
		Map<String, Object> map = new HashMap<>();*/
        /*String time=getMinuteByPointTime("14:00",5);
        System.out.print(time);*/
		/*Long days = DateUtil.dateDiff(DateUtil.getCurrTime(),"day","2020-11-16","yyyy-MM-dd");
		System.out.print(days)*/;
/*		Integer price=37100;
		Integer price2=1855;
		Integer rate=14;
		double a=price.doubleValue()/100-price2.doubleValue()/100;
		double b=(1+rate.doubleValue()/100)*a;
		BigDecimal bg = new BigDecimal(b);
		double a1=bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.print(a1);*/
       /* List<RoomDataEntityVO> array=new ArrayList<>();
		RoomDataEntityVO roomDataEntityVO;
        for(int i=0;i<10000;i++){
			roomDataEntityVO=new RoomDataEntityVO();
			roomDataEntityVO.setGoodId(2000);
			array.add(roomDataEntityVO);
		}
		long start=System.currentTimeMillis();
        int size=array.size();
        for(int i=0;i<size;i++){
        	*//*System.out.print(array.get(i));*//*
		}
		long endTime=System.currentTimeMillis();
        long time=endTime-start;
	    System.out.println("for循环遍历耗费时间为{}"+time);
		long start1=System.currentTimeMillis();
		for(RoomDataEntityVO entityVO:array){
			*//*System.out.print(entityVO);*//*
		}
		long end1=System.currentTimeMillis();
		System.out.println("foreach循环遍历耗费时间为{}"+(end1-start1));
		//map.put("date", DateUtil.);
		if(ObjectUtils.isEmpty("null")){
			System.out.print("123");
		}*/
		/*String s="18:00:00";
		SimpleDateFormat ft = new SimpleDateFormat ("HH:mm");
		try {
			Date date=ft.parse(s);
			System.out.println(date.getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}*/
		/*String day="2020-07-17";
		String time=getNextMothesDay(day);
		System.out.println(time);*/
        //int day=getDay("2020-08-04","2020-09-04");
        //System.out.println(day);
	//	int in = compareDate("2020-08-05 17:03:01", "2020-08-05 17:02:01","yyyy-MM-dd HH:mm:ss");
	//	System.out.printf("==="+in);
/*
		Long difMin = DateUtil.dateDiff("2020-08-05 18:00:00","min","2020-08-05 17:40:00","yyyy-MM-dd HH:mm:ss");
		if(-20<=difMin && difMin<=0){
			System.out.printf("====:"+difMin);
		}else{
			System.out.printf("====difMin:"+difMin);
		}*/

		/*System.out.printf("=="+getCurrTime());*/
		/*Date dt = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String endTime=sdf.format(dt);
		String s="2020-08-31";
		boolean e=DateUtil.commpareDay(endTime,s);
		if(e){
			endTime=s;
		}
		System.out.println(endTime);*/
	/*	double d = (double) 1590/100;
		System.out.println(d);*/

		/*String time=getFrontDay("2020-06-30");
        System.out.println(switchDateParse("/Date(1594116013836+0800)/"));
		System.out.println(switchTimeParse("/Date(1594115740139+0800)/"));

		logger.info(System.currentTimeMillis());
		DateUtil dateUtil = new DateUtil();
		logger.info("____>"+dateUtil.getStartTime().getTime());
		boolean f=commpareDay("2020-07-08","2020-07-06");
        System.out.println(f);*/

/*		System.out.println("时间间隔："+switchTimeSubtractParse("/Date(1596613133094+0800)/","/Date(1596611739000+0800)/"));

		System.out.println("时间间隔："+switchDateParse("/Date(1596613133094+0800)/"));*/

	}
}
